package com.ease.util {

   import flash.events.Event;

   /**
    * This class represents a request to enable or disable some service for the given object.
    * The service is identified by a name property. The interested services are supposed to
    * listen for this event and add or remove the given object from their lists of serviced
    * objects.
    *
    * <p>The object property of this event can be an iterable collection, in which case, the
    * target service is expected to apply the enable/disable request for all objects in the
    * collection.
    */
   public class ServiceApplicabilityUpdateEvent extends ObjectEvent
   {
      /**
       * Event type id to indicate interest in enabling the service for the given object.
       */
      public static const SERVICE_ENABLE_REQUESTED:String = "serviceEnableRequested";

      /**
       * Event type id to indicate interest in disabling the service for the given object.
       */
      public static const SERVICE_DISABLE_REQUESTED:String = "serviceDisableRequested";

      /**
       * Event type id to indicate a one-off processing (or reprocessing) of the given object
       * by the named service.
       */
      public static const SERVICE_PROCESSING_REQUESTED:String = "serviceProcessingRequested";

      /**
       * An option for this event to request that the service should be permanently disabled
       * on the given object, i.e., subsequent <code>serviceEnableRequest</code>s should be
       * ignored.
       */
      public static const PERMANENTLY_DISABLED:uint = 1 << 0;

      /**
       * Initializes an instance of this class.
       *
       * @param type
       *    The id for the event.
       *
       * @param object
       *    Value for the object property.
       *
       * @param serviceId
       *    Id or name of the service that should be enabled or disabled for the given
       *    object.
       *
       * @param bubbles
       *    Whether the event should bubble or not.
       */
      public function ServiceApplicabilityUpdateEvent( type:String, object:Object = null,
                                                       serviceId:String = null,
                                                       options:uint = 0,
                                                       bubbles:Boolean = true ) {
         super( type, object, bubbles );
         _serviceId = serviceId;
         _options = options;
      }

      //---------------------------------------------------------------------------
      // Class Methods

      /**
       * A convenience factory method to create an instance of this class that represents
       * a request to enable the specified service on the given object.
       */
      public static function
         newEnableRequest( object:Object, serviceId:String ):ServiceApplicabilityUpdateEvent {
         return new ServiceApplicabilityUpdateEvent( SERVICE_ENABLE_REQUESTED, object,
            serviceId );
      }

      /**
       * A convenience factory method to create an instance of this class that represents
       * a request to disable the specified service on the given object.
       */
      public static function
         newDisableRequest( object:Object, serviceId:String ):ServiceApplicabilityUpdateEvent {
         return new ServiceApplicabilityUpdateEvent( SERVICE_DISABLE_REQUESTED, object,
            serviceId );
      }

      /**
       * A convenience factory method to create an instance of this class that represents
       * a request to process the given object by the specified service.
       */
      public static function newProcessingRequest(
         object:Object, serviceId:String ):ServiceApplicabilityUpdateEvent {
         return new ServiceApplicabilityUpdateEvent( SERVICE_PROCESSING_REQUESTED, object,
            serviceId );
      }

      /**
       * A convenience factory method to create an instance of this class that represents
       * a request to permanently disable the specified service on the given object.
       */
      public static function newPermanentDisableRequest( object:Object,
                                                         serviceId:String ):ServiceApplicabilityUpdateEvent {
         return new ServiceApplicabilityUpdateEvent( SERVICE_DISABLE_REQUESTED, object,
            serviceId, PERMANENTLY_DISABLED );
      }

      //-------------------------------------------------------
      // Property: serviceId

      private var _serviceId:String;

      /**
       * Identifies the service that should be enabled or disabled on the given object.
       */
      public function get serviceId():String {
         return _serviceId;
      }

      //-------------------------------------------------------
      // Property: options

      private var _options:uint;

      /**
       * Various options or settings (e.g., PERMANENTLY_DISABLED) that apply to this request.
       */
      public function get options():uint {
         return _options;
      }

      //---------------------------------------------------------------------------
      // Overridden Methods

      /**
       * Creates a clone of this event. All custom events should override this method.
       *
       * @return
       *    A clone of this event.
       */
      public override function clone():Event {
         return new ServiceApplicabilityUpdateEvent( type, super.object, _serviceId,
            _options, super.bubbles );
      }
   }

}